//
//  YZLShowOrHideView.m
//  Car
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 岳明. All rights reserved.
//

#pragma mark - **订阅的 collectionView

#import "YZLShowOrHideView.h"
#import "Macro.h"
#import "YZLTagCell.h"

@interface YZLShowOrHideView () <UICollectionViewDataSource, UICollectionViewDelegate>



@end

@implementation YZLShowOrHideView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.itemSize = CGSizeMake(kWidth / 5, kWidth / 15);
    flow.minimumLineSpacing = kWidth / 30;
    flow.sectionInset = UIEdgeInsetsMake(kWidth / 30, 15, kWidth / 30, 15);

    if (self.array.count % 4 == 0) {
         self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth / 15 * (self.array.count / 4) + kWidth / 30  * (self.array.count / 4 + 0.5)) collectionViewLayout:flow];
    } else  self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth / 15 * (self.array.count / 4 + 1) + kWidth / 30  * (self.array.count / 4 + 1.5)) collectionViewLayout:flow];
   
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor colorWithRed:247 / 255.f green:247 / 255.f blue:247 / 255.f alpha:1];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[YZLTagCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    

}

- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = [self.array objectAtIndex:indexPath.item];
    YZLTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    if (self.isShow == YES && indexPath.item == 0) {
        cell.backgroundColor = [UIColor colorWithRed:247 / 255.f green:247 / 255.f blue:247 / 255.f alpha:1];
        cell.tagLabel.layer.borderWidth = 0.0;
    } else {
        cell.backgroundColor = [UIColor whiteColor];
        cell.tagLabel.layer.borderColor = [UIColor colorWithWhite:.925 alpha:1].CGColor;
        cell.tagLabel.layer.borderWidth = 1.0;
        cell.tagLabel.layer.cornerRadius = 3.0;
        cell.tagLabel.layer.masksToBounds = YES;
    }
    cell.tagLabel.text = [dic objectForKey:@"title"];
    
    return cell;
}

- (void)setArray:(NSArray *)array {
    if (_array != array) {
        _array = array;
    }
    

    [_collectionView reloadData];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate clickOnDeleteOrAddWithIndexPath:indexPath withView:self];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
